package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjAuthor;
import com.zb.entity.dto.AuthorDto;
import com.zb.entity.vo.AuthorListVo;
import com.zb.entity.vo.AuthorVo;
import com.zb.mapper.ZjAuthorMapper;
import com.zb.search.SearchDto;
import com.zb.service.ZjAuthorService;
import com.zb.utils.PageUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 系统权限菜单表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-02-17
 */
@Service
public class ZjAuthorServiceImpl extends ServiceImpl<ZjAuthorMapper, ZjAuthor> implements ZjAuthorService {
    @Resource
    private ZjAuthorMapper authorMapper;

    // 新增或修改权限
    @Override
    public void save(AuthorDto authorDto) {
        Assert.isNull(authorMapper.existByName(authorDto.getId(), authorDto.getParentId(), authorDto.getName()), "权限名称已存在");
        Assert.isNull(authorMapper.existByCode(authorDto.getId(), authorDto.getParentId(), authorDto.getCode()), "权限编码已存在");
        ZjAuthor author = new ZjAuthor();
        BeanUtil.copyProperties(authorDto, author);
        author.setDisabled(authorDto.getStatus());
        if (null != author.getId()) {
            this.updateById(author);
            return;
        }
        this.save(author);
    }

    // 获取菜单权限列表
    @Override
    public Map<String, Object> getList(SearchDto search) {
        PageUtil.setPageLimit(search);
        long count = authorMapper.getListCount(search);
        if (count <= 0) {
            return PageUtil.pageList(search.getPage(), search.getLimit(), count, new ArrayList<>());
        }
        List<AuthorListVo> voList = authorMapper.getList(search);
        return PageUtil.pageList(search.getPage(), search.getLimit(), count, voList);
    }

    // 批量启用或禁用菜单权限
    @Override
    public void updateDisabledBatchById(List<Long> ids, boolean status) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        authorMapper.updateDisabledBatchById(ids, status);
    }

    // 获取所有菜单权限
    @Override
    public List<AuthorVo> getAllAuthor() {
        QueryWrapper<ZjAuthor> wrapper = new QueryWrapper<>();
        wrapper.eq("is_disabled", false)
                .orderByAsc("parent_id", "sort");
        List<ZjAuthor> authorList = this.list(wrapper);
        return authorList.stream().filter(f -> f.getParentId() == 0).map(author -> {
            AuthorVo vo = new AuthorVo();
            BeanUtil.copyProperties(author, vo);
            vo.setStatus(author.getDisabled());
            vo.setAuthorVos(getChildAuthor(vo.getId(), authorList));
            return vo;
        }).toList();
    }

    private List<AuthorVo> getChildAuthor(Long parentId, List<ZjAuthor> authorList) {
        return authorList.stream().filter(f -> Objects.equals(f.getParentId(), parentId)).map(author -> {
            AuthorVo vo = new AuthorVo();
            BeanUtil.copyProperties(author, vo);
            vo.setStatus(author.getDisabled());
            vo.setAuthorVos(getChildAuthor(vo.getId(), authorList));
            return vo;
        }).toList();
    }
}
